package gui.product;

import facades.ProductFacade;
import gui.common.*;

import java.util.Observer;
import java.util.Observable;

import coreModel.Barcode;
import coreModel.Product;
import coreModel.ProductGroup;
import coreModel.Supply;
import coreModel.Unit;

/**
 * Controller class for the edit product view.
 */
public class EditProductController extends Controller 
implements IEditProductController {


	private ProductData target;
	boolean countChanged;
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the edit product view
	 * @param target Product being edited
	 */
	public EditProductController(IView view, ProductData target) {
		super(view);
		this.target=target;
		countChanged=false;
		construct();
		//can not change the barcode at this point
		getView().enableBarcode(false);
	}

	//
	// Controller overrides
	//

	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditProductView getView() {
		return (IEditProductView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{

		sizeUnitLogic();
		boolean enable=true;
		try{
			//checks to see if the supply can be created
			if(getView().getSizeUnit()==SizeUnits.Count)
			{
				Supply tmpSupply = new Supply(Integer.parseInt
						(getView().getSizeValue()),Unit.COUNT);
			}
			else
			{
				Supply tmpSupply = new Supply(Float.parseFloat(getView().getSizeValue()),
						DataConverter.toEnum(getView().getSizeUnit()));
				Float supplyValue= Float.parseFloat(getView().getSizeValue());
				//added for pounds, gallons .... not = 0
				if(supplyValue<=0)
					enable=false;
			}
			
			if(enable && getView().getDescription().length()>0)
			{
				enable=enable&true;
			}
			else
				enable=false;

			int shelfLife=Integer.parseInt(getView().getShelfLife());
			int threeMonth=	Integer.parseInt(getView().getSupply());
			// added to fix bug 32 "Edit Product 3 Month Supply"
			if(shelfLife<0 || threeMonth<0)
				enable = false;
		}
		catch(Exception e)
		{

			enable =false;
		}

		getView().enableOK(enable);

	}

	private void sizeUnitLogic()
	{
		if(getView().getSizeUnit()==SizeUnits.Count)
		{
			countChanged=false;
			getView().enableSizeValue(false);
			getView().setSizeValue("1");
		}
		else
		{
			if(countChanged==false)
			{
				getView().setSizeValue("0");
				countChanged=true;
			}
			getView().enableSizeValue(true);

		}
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		if(target==null)
			return;
		getView().enableOK(true);
		try
		{
			getView().setBarcode(target.getBarcode());
			getView().setDescription(target.getDescription());
			//used the target so that the value doesn't include the month
			getView().setShelfLife(Integer.toString(((Product) target.getTag()).
					getShelfLife()));
			getView().setSizeUnit(target.getSizeUnit());
			getView().setSizeValue(target.getSize());
			getView().setSupply(target.getSupply());

			Unit unitType= ((Product) target.getTag()).getSupply().getSupplyUnit();
			if(unitType==Unit.COUNT)
			{
				getView().setSizeValue(""+((int)((Product)target.getTag()).
						getSupply().getSupplySize()));
				getView().setSizeUnit(SizeUnits.Count);
			}
			else
			{
				getView().setSizeValue(""+(((Product)target.getTag()).
						getSupply().getSupplySize()));
				getView().setSizeUnit(DataConverter.toEnum(unitType));
				//added this to fix bug 219 this true statement means that in the enable function
				//the size doesn't get written over with zero
				countChanged=true;
			}

		}
		catch (Exception e)
		{
			System.err.println("error Loading Product Group");
		}
	}


	//
	// IEditProductController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit product view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		enableComponents();
	}

	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit product view.
	 */
	@Override
	public void editProduct() 
	{
		try
		{
			Product orginal = (Product) target.getTag();
			Supply tmpSupply;
			SizeUnits unit = getView().getSizeUnit();

			if(unit==SizeUnits.Count)
			{
				tmpSupply= new Supply(1,Unit.COUNT);
			}
			else
			{
				tmpSupply= new Supply(Float.parseFloat(getView().getSizeValue()),
						DataConverter.toEnum(getView().getSizeUnit()));
			}


			Product tmpProduct = new Product(getView().getDescription(),
					new Barcode(getView().getBarcode()));
			tmpProduct.setShelfLife(Integer.parseInt(getView().getShelfLife()));
			tmpProduct.setThreeMonthSupply(Integer.parseInt(getView().getSupply()));
			tmpProduct.setSupply(tmpSupply);
			ProductFacade.getInstance().editProduct(orginal,tmpProduct);

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}




}

